package com.yingside.game;

import com.yingside.collection.game.Equipment;

import java.util.List;
import java.util.Scanner;

public class Game {
    private Hero h;
    private Store store;
    private Monster [] ms = new Monster[5];
    private Vampire [] vs = new Vampire[5];

    private Scanner input = new Scanner(System.in);

    public Game(){
        System.out.println("=====初始化英雄=====");
        h = new Hero("王迪宁",100,10,3,100000);
        System.out.println("=====初始化怪兽=====");
        for (int i = 0; i < ms.length; i++) {
            ms[i] = new Monster(GameUtil.gameRandom(1,4));
            vs[i] = new Vampire(GameUtil.gameRandom(1,4));
        }
        System.out.println("=====初始化商店=====");
        store = new Store();
    }


    public void menu(){
        System.out.println("=====英雄打怪兽游戏=====");
        System.out.println("===== 1、查看属性  =====");
        System.out.println("===== 2、逛商店  =====");
        System.out.println("===== 3、背包管理  =====");
        System.out.println("===== 4、打怪赚钱  =====");
        System.out.println("请选择(1-4)");
        int m = input.nextInt();
        switch (m){
            case 1:
                break;
            case 2:
                shopping();
                break;
            case 3:
                equipmentManagement();
                break;
            case 4:
                fight();
                break;
        }
    }

    private void equipmentManagement() {
        showEquipment();

        // 序号，为了用户好选择
        int index = 1;

        //这里背包中装备的信息应该全部显示出来
        //意思是包括武器，护甲，盾牌，鞋子等等
        //从一开始就进行编号，一直到最后
        //要让用户选择编号，然后再装备相应的内容

        System.out.println("背包中的装备信息:");
        System.out.println("序号\t名称\t功效\t价格\t库存");
        //先把武器循环出来
        List<Weapon> weaponBag = h.getWeaponBag();
        for (int i = 0; i < weaponBag.size(); i++) {
            System.out.println((index ++) + "\t"
                + weaponBag.get(i).getName() + "\t"
                + weaponBag.get(i).getEffect() + "\t");
        }

        //武器背包循环完成之后继续循环其他的背包
        List<Armor> armorBag = h.getArmorBag();
        for (int i = 0; i < armorBag.size(); i++) {
            System.out.println((index ++) + "\t"
                    + armorBag.get(i).getName() + "\t"
                    + armorBag.get(i).getEffect() + "\t");
        }

        //还要继续循环盾牌和鞋子.....

        System.out.println("请选择(1-"+ (index-1) + "),选择0，返回主菜单");

        int m = input.nextInt();


        if(index == 1){
            System.out.println("你的背包空空如也~~~请先去逛一下商店");
            menu();
        }
        else if(m == 0){
            menu();
        }
        else if(m < 0 || m > index){
            System.out.println("你选择的序号出错，请重新选择!");
            equipmentManagement();
        }
        else{
            //如果用户的选择在武器背包的范围之内，就直接装备武器
            if(m >= 1 && m <= weaponBag.size()){
                h.dressEquipment(weaponBag.get(m-1));

            }
            //如果用户的选择在护甲背包的范围之内，就直接装备护甲
            else if (m <= weaponBag.size() + armorBag.size() ){
                h.dressEquipment(armorBag.get(m-1));
            }

            equipmentManagement();
        }

    }

    private void showEquipment(){
        System.out.println("身上已经有的装备:");
        if(h.getEquipment().getWeapon() == null){
            System.out.println("武器: 无");
        }
        else{
            System.out.println("武器: 名称-" + h.getEquipment().getWeapon().getName() + ",功效-" + h.getEquipment().getWeapon().getEffect());
        }

        if (h.getEquipment().getArmor() == null){
            System.out.println("护甲: 无");
        }
        else{
            //已经装备的护甲的信息
        }

        if (h.getEquipment().getShield() == null){
            System.out.println("盾牌: 无");
        }
        else{
            //已经装备的盾牌的信息
        }

        if (h.getEquipment().getShoes() == null){
            System.out.println("鞋子: 无");
        }
        else{
            //已经装备的鞋子的信息
        }
    }

    private void shopping() {
        System.out.println("请选择要购买的类别");
        System.out.println("1、武器");
        System.out.println("2、护甲");
        System.out.println("3、盾牌");
        System.out.println("4、鞋子");
        System.out.println("1-4");
        int m = input.nextInt();
        switch (m){
            case 1:
                buyWeapon();
                break;
        }
    }

    private void buyWeapon() {
        System.out.println("=====欢迎你选购武器=====");
        System.out.println("序号\t名称\t功效\t价格\t库存");
        for (int i = 0; i < store.getWeaponList().size(); i++) {
            Weapon weapon = store.getWeaponList().get(i);
            System.out.println((i+1) + "\t"
                + weapon.getName() + "\t"
                + weapon.getEffect() + "\t"
                + weapon.getPrice() + "\t"
                + weapon.getNum());
        }

        System.out.println("请选择你需要购买的武器(1-"+store.getWeaponList().size()+"),按0返回主菜单");
        int index = input.nextInt();

        if(index == 0){
            menu();
        }
        else if(index < 1 || index > store.getWeaponList().size()){
            System.err.println("你输入的武器编码不正确,请重新输入");
            buyWeapon();
        }
        else{
            Weapon w = store.getWeaponList().get(index-1);
            boolean flag = store.sell(w, h);
            if(flag){
                System.out.println("~~~购买成功~~~");
                System.out.println(h.getName() + "购买了：" + w.getName() + ",花费了：" + w.getPrice());
                System.out.println(h.getName() + "当前剩余金额:" + h.getOwnMoney());
            }
            buyWeapon();
        }

    }



    public Enemy getRandomEnemy(){
        Enemy e = null;
        int m = GameUtil.gameRandom(1,2);
        int r = GameUtil.gameRandom(0,ms.length-1);
        switch (m){

            case 1:
                e = ms[r];
                break;
            case 2:
                e = vs[r];
                break;
        }
        return e;
    }

    private void fight() {
        Enemy e = getRandomEnemy();
        System.out.println("=====开始打怪=====");
        System.out.println("=====" + h.getName() + "V S" + e.getName() + "=====");

        String token = "hero";

        while(true){
                if("hero".equals(token)){
                    //英雄打怪兽
                    h.fight(e);

                    //交换令牌
                    token = "monster";
                }
                else{
                    //怪兽反击英雄
                    e.response(h);

                    token = "hero";
                }

                try {
                    //让当前主程序睡眠 500毫秒
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }

                if(h.getHp() <= 0){
                    System.out.println("胜负乃兵家常事，英雄请重新来过");
                    break;
                }

                if(e.getHp() <= 0){
                    System.out.println(e.getName() + "怪兽死亡...英雄胜利");
                    System.out.println(e.getName() + "掉落金钱:" + e.getMoney());
                    //怪兽死亡，英雄金钱增加
                    h.setOwnMoney(h.getOwnMoney() + e.getMoney());
                    System.out.println("===================================\n\n\n");
                    break;
                }
            }

    }


    public static void main(String[] args) {

        Game g = new Game();
        g.menu();

//        Monster [] ms = new Monster[5];
//
//        for (int i = 0; i < ms.length; i++) {
//            ms[i] = new Monster(GameUtil.gameRandom(1,4));
//        }
//
//        Hero h = new Hero("伟哥",100,12,6);
//
//
//        String token = "hero";
//
//        for (int i = 0; i < ms.length; i++) {
//            Monster m = ms[i];
//            while(true){
//                if("hero".equals(token)){
//                    //英雄打怪兽
//                    h.fight(m);
//
//                    //交换令牌
//                    token = "monster";
//                }
//                else{
//                    //怪兽反击英雄
//                    m.response(h);
//
//                    token = "hero";
//                }
//
//                try {
//                    //让当前主程序睡眠 500毫秒
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                if(h.getHp() <= 0){
//                    System.out.println("胜负乃兵家常事，英雄请重新来过");
//                    break;
//                }
//
//                if(m.getHp() <= 0){
//                    System.out.println(m.getName() + "怪兽死亡...英雄胜利,休息1分钟，回血继续");
//                    h.setHp(100);
//                    System.out.println("===================================\n\n\n");
//                    break;
//                }
//            }
//
//            if(h.getHp() <= 0){
//                break;
//            }
//
//        }
        

        /*
        Monster m = ms[GameUtil.gameRandom(0,4)];

        while(true){
            if("hero".equals(token)){
                //英雄打怪兽
                h.fight(m);

                //交换令牌
                token = "monster";
            }
            else{
                //怪兽反击英雄
                m.response(h);

                token = "hero";
            }

            try {
                //让当前主程序睡眠 500毫秒
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(h.getHp() <= 0){
                System.out.println("胜负乃兵家常事，英雄请重新来过");
                break;
            }

            if(m.getHp() <= 0){
                System.out.println("怪兽死亡...英雄胜利");
                break;
            }

        }
        */



    }
}
